package com.road.yishi.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.road.entity.QuestData;
import com.road.entity.base.Option;
import com.road.yishi.dao.QuestDataDao;
import com.road.yishi.db.DbParameter;
import com.road.yishi.db.StrategyBaseDao;

@Repository
public class QuestDataDaoImpl extends StrategyBaseDao implements QuestDataDao {

	@Override
	public boolean addQuestData(QuestData info) {
		if (info == null) {
			return false;
		}
		boolean result = false;
		if (info.beginAdd()) {
			String sql = "  REPLACE INTO t_u_questdata(`UserId`, `TemplateId`, `IsComplete`,`AddDate`, `CompletedDate`, `Condition1`, `Condition2`, `Condition3`, `Condition4`, `RepeatFinish`, `IsSelect`, `IsExist`)  VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);";
			Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
			para.put(1, new DbParameter(Types.INTEGER, info.getUserId()));
			para.put(2, new DbParameter(Types.INTEGER, info.getTemplateId()));
			para.put(3, new DbParameter(Types.BOOLEAN, info.getIsComplete()));
			para.put(4, new DbParameter(Types.TIMESTAMP, info.getAddDate()));
			para.put(5, new DbParameter(Types.TIMESTAMP, info.getCompletedDate()));
			para.put(6, new DbParameter(Types.INTEGER, info.getCondition1()));
			para.put(7, new DbParameter(Types.INTEGER, info.getCondition2()));
			para.put(8, new DbParameter(Types.INTEGER, info.getCondition3()));
			para.put(9, new DbParameter(Types.INTEGER, info.getCondition4()));
			para.put(10, new DbParameter(Types.INTEGER, info.getRepeatFinish()));
			para.put(11, new DbParameter(Types.BOOLEAN, info.getIsSelect()));
			para.put(12, new DbParameter(Types.BOOLEAN, info.getIsExist()));
			result = execNoneQuery(sql, para) > -1 ? true : false;
			info.commitAdd(result);
		}
		return result;
	}

	@Override
	public List<QuestData> getQuestDatas(int userId) {
		String sqlText = "     SELECT * FROM t_u_questdata WHERE UserId=? And IsExist=true;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.INTEGER, userId));
		PreparedStatement pstmt = execQuery(sqlText, para);
		ResultSet rs = null;
		List<QuestData> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<QuestData>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					QuestData info = new QuestData();
					info.setUserId(rs.getInt("UserId"));
					info.setTemplateId(rs.getInt("TemplateId"));
					info.setIsComplete(rs.getBoolean("IsComplete"));
					info.setAddDate(rs.getTimestamp("AddDate"));
					info.setCompletedDate(rs.getTimestamp("CompletedDate"));
					info.setCondition1(rs.getInt("Condition1"));
					info.setCondition2(rs.getInt("Condition2"));
					info.setCondition3(rs.getInt("Condition3"));
					info.setCondition4(rs.getInt("Condition4"));
					info.setRepeatFinish(rs.getInt("RepeatFinish"));
					info.setIsExist(rs.getBoolean("IsExist"));
					info.setIsSelect(rs.getBoolean("IsSelect"));
					info.setOp(Option.None);
					infos.add(info);
				}
			} catch (SQLException e) {
				infos = null;
				Log.error("执行出错" + sqlText, e);
			}finally {
				closeConn(pstmt, rs);
			}
		} 
		return infos;
	}

	@Override
	public boolean updateQuestData(QuestData info, byte[] questSite) {
		if (info == null)
			return false;

		boolean result = false;
		if (info.getOp() != Option.None) {
			if (getQuestData(info.getUserId(), info.getTemplateId()) == false) {
				info.setOp(Option.Insert);
			} else {
				info.resetUpdate();
			}
			
			if (info.getOp() == Option.Insert) {
				result = addQuestData(info);
			}
			if (info.getOp() == Option.Update) {
				result = updateQuestData(info);
			}
		}
		return result;
	}

	@Override
	public boolean getQuestData(int userId, int templateId) {
		String sql = "Select IFNULL(Count(*),0)  From t_u_questdata Where UserId=? And TemplateId=?;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.INTEGER, userId));
		para.put(2, new DbParameter(Types.INTEGER, templateId));
		int count = execCountQuery(sql , para);
		return count > 0 ? true : false;
	}

	@Override
	public boolean updateQuestData(QuestData info) {
		boolean result = false;
		if (info.beginUpdate()) {
			String sql = "UPDATE t_u_questdata SET `IsComplete`=?, `AddDate`=?,`CompletedDate`=?, `Condition1`=?, `Condition2`=?, `Condition3`=?, `Condition4`=?, `RepeatFinish`=?, `IsSelect`=?, `IsExist`=? WHERE UserId=? And TemplateId=?; ";
			Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
			para.put(1, new DbParameter(Types.BOOLEAN, info.getIsComplete()));
			para.put(2, new DbParameter(Types.TIMESTAMP, info.getAddDate()));
			para.put(3, new DbParameter(Types.DATE, info.getCompletedDate()));
			para.put(4, new DbParameter(Types.INTEGER, info.getCondition1()));
			para.put(5, new DbParameter(Types.INTEGER, info.getCondition2()));
			para.put(6, new DbParameter(Types.INTEGER, info.getCondition3()));
			para.put(7, new DbParameter(Types.INTEGER, info.getCondition4()));
			para.put(8, new DbParameter(Types.INTEGER, info.getRepeatFinish()));
			para.put(9, new DbParameter(Types.BOOLEAN, info.getIsSelect()));
			para.put(10, new DbParameter(Types.BOOLEAN, info.getIsExist()));
			para.put(11, new DbParameter(Types.INTEGER, info.getUserId()));
			para.put(12, new DbParameter(Types.INTEGER, info.getTemplateId()));
			result = execNoneQuery(sql, para) > -1 ? true : false;
			info.commitUpdate(result);
		}
		return result;

	}

	@Override
	public boolean updateQuestSite(int userId, byte[] questSite) {
		String sqlUpdate = "Update t_u_playerinfo Set QuestSite=? Where UserId=?;";
		Map<Integer, DbParameter> paraUpdate = new HashMap<Integer, DbParameter>();
		paraUpdate.put(1, new DbParameter(Types.BINARY, questSite));
		paraUpdate.put(2, new DbParameter(Types.INTEGER, userId));
		return (execNoneQuery(sqlUpdate, paraUpdate) <= -1) ? false : true;
	}
}
